using UnityEngine;

public class WeaponFight : MonoBehaviour
{
	public Transform m_transform;

	public Transform fighttarget;

	public bool ismachinegun;

	public bool isfight;

	public Transform shotpoint;

	public bool isfriendweapon;

	public float fightdisance = 2000f;

	private float machineshotspeed = 900f;

	public bool isshot;

	private float intervaltime = 8f;

	private float curtime;

	public Fri_AIController target_fri;

	public AIController target_ai;

	public int parent_num;

	private Vector3 dir;

	private float angle;

	private Vector3 playeraim;

	private bulletmove curbullet;

	private missilemove curmissile;

	private int curbulletnum;

	private int maxbulletnum = 10;

	public int hurtdamage;

	public int penetrationVal;

	private void Start()
	{
		m_transform = base.transform;
	}

	private void Update()
	{
		if (fighttarget != null)
		{
			if (Vector3.Distance(m_transform.position, fighttarget.position) < fightdisance)
			{
				isfight = true;
			}
			else
			{
				isfight = false;
			}
		}
		else
		{
			isfight = false;
		}
		if (isshot)
		{
			curtime += Time.deltaTime;
			if (curtime > intervaltime)
			{
				isshot = false;
				curtime = 0f;
				curbulletnum = 0;
			}
		}
		if (isfight && !isshot)
		{
			fight();
		}
	}

	public void fight()
	{
		Vector3 position = fighttarget.position;
		float y = position.y;
		Vector3 position2 = m_transform.position;
		if (!(y - position2.y > 10f))
		{
			return;
		}
		dir = fighttarget.position - m_transform.position;
		dir.y = 0f;
		m_transform.forward = dir;
		if (isfriendweapon)
		{
			if (ismachinegun)
			{
				shotpoint.LookAt(fighttarget);
				curbullet = bulletpool.instance.GetBullet();
				if (curbullet != null)
				{
					playeraim = target_ai.CalcAimPos(m_transform.position, machineshotspeed);
					curbullet.transform.position = shotpoint.position;
					curbullet.transform.rotation = shotpoint.rotation;
					curbullet.targetAI = null;
					curbullet.isplayerbullet = true;
					curbullet.movespeed = machineshotspeed;
					curbullet.endpos = playeraim;
					curbullet.shot(hurtdamage, penetrationVal, parent_num, isfri: true);
					curbulletnum++;
				}
				if (curbulletnum > maxbulletnum)
				{
					isshot = true;
					curtime = 0f;
				}
				return;
			}
			angle = Vector3.Angle(m_transform.forward, fighttarget.position - m_transform.position);
			if (!(angle > 10f) || !(angle < 40f))
			{
				return;
			}
			Vector3 vector = fighttarget.InverseTransformPoint(m_transform.position);
			if (vector.z < -100f)
			{
				shotpoint.LookAt(fighttarget);
				curmissile = missilepool.instance.GetMissile();
				if (curmissile != null)
				{
					curmissile.transform.position = shotpoint.position;
					curmissile.transform.rotation = shotpoint.rotation;
					curmissile.targetAI = target_ai;
					curmissile.isplayerbullet = true;
					curmissile.shot(hurtdamage, penetrationVal, parent_num, 120f);
					isshot = true;
					curtime = 0f;
				}
			}
			return;
		}
		if (ismachinegun)
		{
			shotpoint.LookAt(fighttarget);
			curbullet = NPCbulletpool.instance.GetBullet();
			if (curbullet != null)
			{
				if (target_fri != null)
				{
					playeraim = target_fri.CalcAimPos(m_transform.position, machineshotspeed);
				}
				else
				{
					playeraim = plane_smothfollow.instance.playerAim(m_transform.position, machineshotspeed);
				}
				curbullet.transform.position = shotpoint.position;
				curbullet.transform.rotation = shotpoint.rotation;
				curbullet.targetAI = null;
				curbullet.isplayerbullet = false;
				curbullet.movespeed = machineshotspeed;
				curbullet.endpos = playeraim;
				curbullet.shot(hurtdamage, penetrationVal, parent_num);
				curbulletnum++;
			}
			if (curbulletnum > maxbulletnum)
			{
				isshot = true;
				curtime = 0f;
			}
			return;
		}
		angle = Vector3.Angle(m_transform.forward, fighttarget.position - m_transform.position);
		if (!(angle > 10f) || !(angle < 40f))
		{
			return;
		}
		Vector3 vector2 = fighttarget.InverseTransformPoint(m_transform.position);
		if (vector2.z < -100f)
		{
			shotpoint.LookAt(fighttarget);
			curmissile = NPCmissilepool.instance.GetMissile();
			if (curmissile != null)
			{
				curmissile.transform.position = shotpoint.position;
				curmissile.transform.rotation = shotpoint.rotation;
				curmissile.targetAI = null;
				curmissile.targetai_fri = target_fri;
				curmissile.targetTra = fighttarget;
				curmissile.isplayerbullet = false;
				curmissile.shot(hurtdamage, penetrationVal, parent_num, 120f);
				isshot = true;
				curtime = 0f;
			}
		}
	}
}
